﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Extensil.HotKeys
{
    internal enum SupportedModifiers
    {
        CTRL,
        SHIFT,
        ALT
    }
    internal enum SupportedKeys
    {
        A,
        B,
        C,
        D,
        E,
        F,
        G,
        H,
        I,
        J,
        K,
        L,
        M,
        N,
        O,
        P,
        Q,
        R,
        S,
        T,
        U,
        V,
        W,
        X,
        Y,
        Z,
        D1,
        D2,
        D3,
        D4,
        D5,
        D6,
        D7,
        D8,
        D9,
        D0,
        F1,
        F2,
        F3,
        F4,
        F5,
        F6,
        F7,
        F8,
        F9,
        F10,
        F11,
        F12,
    }
    internal class HotKeyManager
    {
        public static Keys Parse(string hotKeyText)
        {
            string[] keys = hotKeyText.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            Keys retValue = Keys.None;
            foreach(string key in keys)
            {
                switch(key.ToLower())
                {
                    case "ctrl":
                        retValue = retValue | Keys.Control;
                        break;
                    case "alt":
                        retValue = retValue | Keys.Alt;
                        break;
                    case "shift":
                        retValue = retValue | Keys.Shift;
                        break;
                    default:
                        string k = key;
                        if(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }.Contains<string>(key))
                            k = "D" + key;
                        retValue = retValue | (Keys)Enum.Parse(typeof(Keys), k);
                        break;
                }
            }
            return retValue;
        }
        public static int GetHotKeys(int lParam)
        {
            if((Keys)(lParam << 16) == Keys.Alt)
                lParam = ((lParam >> 4) << 4) | 1;
            else if((Keys)(lParam << 16) == Keys.Shift)
                lParam = ((lParam >> 4) << 4) | 4;
            return RotateLeft(lParam, 16);
        }
        static int RotateLeft(int value, int count)
        {
            return (value << count) | (value >> (32 - count));
        }
    }
}
